/*
 * Copyright (C) 2008-2011 CBDP Consortium
 * 
 * All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.
 * 
 * This software consists of contributions made by many individuals, 
 * listed below:
 *
 * Author: 	Xabier Laiseca <xabier.laiseca@deusto.es>
 * 			David Ausín <david.ausin@deusto.es>
 */
package cbdp.server.contextreasoner.impl.contextmanager.online.wrappers;

import java.io.InputStream;
import java.util.Collection;
import java.util.Map;

import cbdp.server.contextreasoner.Task;
import cbdp.server.contextreasoner.exceptions.ContextManagerException;
import cbdp.server.contextreasoner.impl.reasoner.GenericReasoner;
import cbdp.server.contextreasoner.internal.InternalContextManager;
import cbdp.server.contextreasoner.model.Instance;
import cbdp.server.contextreasoner.model.exceptions.NotExistingInstanceException;

public class ContextManagerRuleLaunchingWrapper implements InternalContextManager
{
	private InternalContextManager wrapped;
	
	public void setContextManager(final InternalContextManager wrapped)
	{
		this.wrapped = wrapped;
	}
	
	
	private void launchRulesIfNotInExecution()
	{
		if (!isInExecution())
		{
			wrapped.getGenericReasoner().launchRuleSets();
		}
	}
	
	@Override
	public Map<String, String> getOntologyURIs()
	{
		return wrapped.getOntologyURIs();
	}

	@Override
	@Deprecated
	public void extendOntology(final InputStream ontology, final String namespace) throws ContextManagerException
	{
		wrapped.extendOntology(ontology, namespace);
		launchRulesIfNotInExecution();
	}
	
	@Override
	public void extendOntology(final InputStream ontology, final String namespace, final String ontologyName) throws ContextManagerException
	{
		wrapped.extendOntology(ontology, namespace, ontologyName);
		launchRulesIfNotInExecution();
	}

	@Override
	public void createInstance(final String id, final String type) throws ContextManagerException
	{
		wrapped.createInstance(id, type);
		launchRulesIfNotInExecution();
	}

	@Override
	public void deleteInstance(final String id) throws ContextManagerException, NotExistingInstanceException
	{
		wrapped.deleteInstance(id);
		launchRulesIfNotInExecution();
	}

	@Override
	public Instance getInstance(final String id) throws ContextManagerException, NotExistingInstanceException
	{
		return wrapped.getInstance(id);
	}

	@Override
	public Instance getInstance(final String id, final String type) throws ContextManagerException
	{
		return wrapped.getInstance(id, type);
	}

	@Override
	public Collection<Instance> getInstances(final String clazz) throws ContextManagerException
	{
		return wrapped.getInstances(clazz);
	}

	@Override
	public void execute(final Task task)
	{
		wrapped.execute(task);
		wrapped.getGenericReasoner().launchRuleSets();
	}

	@Override
	public boolean isInExecution()
	{
		return wrapped.isInExecution();
	}

	@Override
	public void unload()
	{
		wrapped.unload();
	}

	@Override
	public GenericReasoner getGenericReasoner()
	{
		return wrapped.getGenericReasoner();
	}
}
